<?php
/*
**智游宝检票通知同步程序
**2015-06-17
**by pzp
*/
$inSide = new SoapClient(null,array(
        "location" => "http://localhost/open/openService/pft_insideMX.php",
        "uri" => "www.16u.com?ac_16u=16ucom|pw_16u=c33367701511b4f6020ec61ded352059|auth_16u=true")
);

include('/var/www/html/new/d/module/api/function.class.php');

if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}

if($_REQUEST['order_no']) exit;  //订单检票通知
/*
http://www.12301.cc/module/api/ZhiYouBao/zyb_check_order.php?order_code=pft144326817493249932&status=success&checkNum=5&returnNum=0&total=5&sign=23b6c64d2c3aa7555f2bd3bb15d0bee7
*/

$pft_ordernum = $_REQUEST['order_code'];//票付通订单号 
$checkNum = $_REQUEST['checkNum']; //已检票数量
$returnNum = $_REQUEST['returnNum']; //退票数量
$total = $_REQUEST['total'];
$sign = $_REQUEST['sign'];
$status = $_REQUEST['status'];

if($pft_ordernum){
	
	// $domain = explode('pft',$pft_ordernum);
	// if(count($domain)<2){ echo 'fail';	exit;} // 不是票付通的订单
	
	$txt=json_encode($_REQUEST);
	logfile($txt,"zyb_checkOrder_data");

	$selorder = "select pftOrder from all_api_order where tempOrder='$pft_ordernum' limit 1";
	$GLOBALS['le']->query($selorder);
	$GLOBALS['le']->fetch_assoc();
	$pftOrder = $GLOBALS['le']->f('pftOrder');
	
	$pft_order = $inSide->Order_Globle_Search(
			0,0,0,0,"",
			"","","","","",
			"","",$pftOrder,"",0,
			"","","",1,0,
			0,1,"","","",
			"","","","","",
			"","","1","","");
	if(strlen($pft_order)>50){
		$pft_order_xml =simplexml_load_string($pft_order);
		$UUlid = (int)$pft_order_xml->Rec->UUlid;
		$UUtnum = (int)$pft_order_xml->Rec->UUtnum;
		$UUsalerid  = (string)$pft_order_xml->Rec->UUsalerid;
		$UUcode  = (string)$pft_order_xml->Rec->UUcode;
		$UUstatus  = (string)$pft_order_xml->Rec->UUstatus;
		$UUaids=	(string)$pft_order_xml->Rec->UUaids;
		if($UUaids == 0){
			$UUaid = (int)$pft_order_xml->Rec->UUaid;
		}else{
			$UUaidss= explode(',',$UUaids);
			$UUaid = $UUaidss[0];
		}
		
		switch ($UUaid){ 		
			case 3311:  //中赫
				$key = "sdzfxhlwh";
			break;
			case 22415:
				$key = "sdzfxsdhyls";
			break;
			case 37809:  //南山大佛
				$key = "sdzfxlypw";
			break;
			case 51667:
				$key = '3665D15A9D73DF3506F51CFD013ABC3B';
			break;
			
			case 50618:
				$key = 'sdzfxlvmamafx';
			break;
			
			case 22845:
				$key = 'sdzfxxmlyjr';
			break;
			
			case 4274: //携程分销
				$key = 'xfcxhs';
			break;
			
			case 27810: //先行网运营部
				$key = '6265C757118A935909641669CF12B212';
			break;
			
			default:
			break;
		}
		$checksign = md5('order_code='.$pft_ordernum.$key);
		if($sign == $checksign && $status =='success'){
			$chkIns = 499;
			if($UUstatus==2) $chkIns = 498;
			$PFT_infunction = new AllFunction();
			$r = $PFT_infunction->Unified_Check_IN($UUsalerid,$UUcode,date('Y-m-d H:i:s'),$chkIns);
			if($r == 100){
				$selorder = "update all_api_order  set oStatus=1 where pftOrder='$pftOrder' limit 1";
				$GLOBALS['le']->query($selorder);
				logfile($pftOrder."|".$r,"zyb_checkOrder_success");
				echo "success";
			}else{
				logfile($pftOrder."|".$r,"zyb_checkOrder_fail");
				echo "fail";
			}
		}
		elseif($sign == $checksign && $status =='cancel'){
			echo "success";
		}
	}
	else echo "fail";
}
else echo "fail";

// function logfile($txt,$logname){
	// $fp = fopen('/var/www/html/new/d/logs/api/ZhiYouBao/'.$logname.date('Y-m-d').'.txt',"a");
	// flock($fp, LOCK_EX) ;
	// fwrite($fp,date('Y-m-d H:i:s')."|".$txt."\n");
	// flock($fp, LOCK_UN);
	// fclose($fp); 
// }
function logfile($word,$path){
	$filepath= '/var/www/html/new/d/logs/api/ZhiYouBao/'.$path.'.txt';
	if(file_exists($filepath)){
		$word = date("Y-m-d H:i:s")."|".$word;
		$word = mb_convert_encoding($word, 'GBK', 'UTF-8');
		$maxsize  = 1024*1024*10;
		$filesize = filesize($filepath);
		$filetype = end(explode(".",$filepath));
		$filename = substr($filepath,0,strpos($filepath,'.'));
		if($filesize>$maxsize)
		{
			rename($filepath, $filename.'_'.date('YmdHis').'.'.$filetype);
			$handler=fopen($filepath,'w');
			flock($handler, LOCK_EX) ;
			fwrite($handler,"\n".$word);
			flock($handler, LOCK_UN);
			fclose($handler);
		}
		else
		{
			$fp = fopen('/var/www/html/new/d/logs/api/ZhiYouBao/'.$path.'.txt',"a");
			flock($fp, LOCK_EX);
			fwrite($fp,$word."\n");
			flock($fp, LOCK_UN);
			fclose($fp);
		}
	}else{
		$fp = fopen('/var/www/html/new/d/logs/api/ZhiYouBao/'.$path.'.txt',"a");
		flock($fp, LOCK_EX);
		fwrite($fp,date('Y-m-d H:i:s')."|".$word."\n");
		flock($fp, LOCK_UN);
		fclose($fp);
	}
}
?>